/**
 * time: 2025-09-16
 * author: XJ
 * description: 产品基本信息页面
 */

import { CheckCircleOutlined, CloseCircleOutlined, InboxOutlined, SyncOutlined, UploadOutlined } from '@ant-design/icons';
import { useLocation, useModel, useNavigate } from '@umijs/max';
import { Button, Cascader, DatePicker, Divider, Form, Input, InputNumber, message, Modal, Row, Select, Space, Steps, TreeSelect, Upload } from 'antd';
import { useEffect, useState } from 'react';
const COMMON_FIELD_STYLE = { width: '100%', height: 40, borderRadius: 4 };
import dayjs from 'dayjs';
import { auditCompany, confirmRate, updateCompany, uploadFile } from '@/services/apis/companyRegister';
import TextArea from 'antd/es/input/TextArea';
const LAST_YEAR = dayjs().subtract(1, 'year').format('YYYY');
const FORM_LAYOUT = {
    labelCol: { span: 6 }, // 调整标签宽度，避免长标签换行
    wrapperCol: { span: 18 },
    layout: 'horizontal' as const,
    
    size: 'middle' as const, // 统一表单控件尺寸
};
const CompanyAllInfosPage: React.FC<{ searchCallFun: any }> = (props) => {
    const {
        modalProps,
        setModalProps,
    } = useModel("WorkOfRate.modal")
    const { initialState, setInitialState } = useModel('@@initialState');
    const [hetongList, setHetongList] = useState<any[]>([])
    const [checkFilesList, setCheckFiles] = useState<any[]>([])
    const [auditDescFilesList, setAuditDescFilesList] = useState<any[]>([])
    const [paywayFilesList, setPaywayFilesList] = useState<any[]>([])
    const [otherFilesList, setOtherFilesList] = useState<any[]>([])
    const [needReason, setNeedReason] = useState<boolean>(false)
    const navigate = useNavigate();
    const loaction = useLocation()
    const [form] = Form.useForm()
    const handleSubmit = () => {
        form.validateFields().then(values => {
            confirmRate({
                ...values,
                companyId: modalProps.record.companyId,
                projectId: modalProps.record.projectId,
                thirdId: initialState?.currentUser.data.id,
                userType: initialState?.currentUser.data.userType,
            }).then(res => {
                const { code } = res;
                if (code == "0") {
                    navigate("/api_status_page", {
                        state: {
                            title: "提交报告",
                            message: "提交报告成功",
                            callbackUrl: loaction.pathname
                        }
                    });
                    setModalProps(prev => ({ ...prev, visible: false }))
                } else {
                    navigate("/api_status_page", {
                        state: {
                            title: "提交报告",
                            message: "提交报告失败",
                            callbackUrl: loaction.pathname
                        }
                    });
                }
            })
        })
    };
    useEffect(() => {
        form.setFieldsValue({ ...modalProps.record, companyChangeTime: dayjs(modalProps.record.companyChangeTime) })
    }, [modalProps.record])
    const onCancel = () => {
        setModalProps(prev => ({ ...prev, visible: false }))
    }

    const handleAutoUpload = async (file: any, formPath: any) => {

        try {
            const response = await uploadFile(file)
            if (response.code == "0") {
                if (formPath == "auditReport" || formPath == "evaluationReport" || formPath == "acceptanceReport") {
                    setHetongList(prev => ([{ name: response.data.filename, uid: Math.random().toString() }]))

                }
                if (formPath == "invoiceReport") {
                    setCheckFiles(prev => ([{ name: response.data.filename, uid: Math.random().toString() }]))
                }

                if (formPath == "auditDescFiles") {
                    setAuditDescFilesList(prev => ([{ name: response.data.filename, uid: Math.random().toString() }]))
                }

                if (formPath == "paywayFiles") {
                    setPaywayFilesList(prev => ([{ name: response.data.filename, uid: Math.random().toString() }]))
                }

                if (formPath == "otherFiles") {
                    setOtherFilesList(prev => ([{ name: response.data.filename, uid: Math.random().toString() }]))
                }
                form.setFieldValue(formPath, [response.data])

            }
        } catch (error) {
            // handleError(error, file);
        }
    };

    return (
        <Modal
            title={modalProps.title}
            open={modalProps.visible}
            onCancel={onCancel}
            footer={[
                <Button key="back" onClick={onCancel}>
                    取消
                </Button>,
                <Button key="submit" type="primary"
                    onClick={handleSubmit}
                >
                    提交
                </Button>,
            ]}
            width={900}
            destroyOnClose
            maskClosable={false}
        >
            <Form
                {...FORM_LAYOUT}
                form={form}
            >
                <div style={{ padding: 20, backgroundColor: '#f8f9fa', boxSizing: 'border-box' }}>
                    <div
                        style={{
                            backgroundColor: '#fff',
                            padding: 20,
                            borderRadius: 8,
                            boxShadow: '0 1px 3px rgba(0,0,0,0.05)',
                        }}
                    >
                        <div
                            style={{
                                fontSize: 16,
                                fontWeight: 500,
                                color: '#1f2937',
                                marginBottom: 12,
                            }}
                        >
                            基本信息
                        </div>
                        <Form.Item
                            label="企业名称"
                            name="companyName"
                            rules={[{ required: true, message: '请输入企业名称' }]}
                            style={{ marginBottom: 20 }}
                        >
                            <Input
                                className={'custom-readonly-input'}
                                disabled={true} placeholder="请输入企业名称" allowClear style={COMMON_FIELD_STYLE} />
                        </Form.Item>
                        {
                            initialState?.currentUser.data.userType == "rate" && (
                                <>
                                    <Form.Item
                                        label="评测时间"
                                        name={"rateTime"}
                                        rules={[{ required: true, message: '请选择评测时间' }]}
                                        style={{ marginBottom: 20 }}
                                    >
                                        <DatePicker />
                                    </Form.Item>
                                    <Form.Item
                                        name="digitalLevel"
                                        label="数字化水平等级"
                                        style={{ marginBottom: 20 }}
                                        rules={[{ required: true, message: '请选择数字化水平等级' }]}

                                    >
                                        <Select
                                            placeholder="请选择数字化水平等级"
                                            style={{ ...COMMON_FIELD_STYLE, flex: 1 }}
                                        // 添加表单绑定属性
                                        // value={form.getFieldValue('digitalLevel')}
                                        // onChange={(value) => form.setFieldValue('digitalLevel', value)}
                                        >
                                            <Select.Option value="level1">一级</Select.Option>
                                            <Select.Option value="level2">二级</Select.Option>
                                            <Select.Option value="level3">三级</Select.Option>
                                            <Select.Option value="level4">四级</Select.Option>
                                        </Select>
                                    </Form.Item>
                                    <Form.Item
                                        name="effect"
                                        label="数字化基础、管理及成效得分"
                                        rules={[{ required: true, message: '请输入数字化基础、管理及成效得分', pattern: /^\d+(\.\d+)?$/ }]}
                                        style={{ marginBottom: 20 }}
                                    >
                                        <Input placeholder='请输入数字化基础、管理及成效得分' style={COMMON_FIELD_STYLE} />
                                    </Form.Item>

                                    <Form.Item
                                        label="等级评测证明"
                                        name={"evaluationReport"}
                                        getValueProps={(values) => {
                                            return { values: values }
                                        }}
                                        rules={[{ required: true, message: '请上传等级评测证明' }]}
                                        style={{ marginBottom: 20 }}
                                    >
                                        <Upload
                                            fileList={hetongList}
                                            beforeUpload={(file) => {
                                                handleAutoUpload(file, "evaluationReport");
                                                return false;
                                            }}
                                            listType="text"
                                            maxCount={1}
                                            style={COMMON_FIELD_STYLE}
                                        >
                                            <Button icon={<UploadOutlined />}>点击上传等级评测证明</Button>
                                        </Upload>
                                    </Form.Item>
                                </>
                            )
                        }
                        {
                            initialState?.currentUser.data.userType == "audit" && (
                                <>
                                    <Form.Item
                                        label="审计时间"
                                        name={"auditTime"}
                                        rules={[{ required: true, message: '请选择审计时间' }]}
                                        style={{ marginBottom: 20 }}
                                    >
                                        <DatePicker />
                                    </Form.Item>
                                    <Form.Item
                                        name="transformationInvestment"
                                        label="改造总花费(万元)"
                                        rules={[{ required: true, message: '请输入改造总花费', pattern: /^\d+(\.\d+)?$/ }]}
                                        style={{ marginBottom: 20 }}
                                    >
                                        <Input placeholder='请输入改造总花费' style={COMMON_FIELD_STYLE} />
                                    </Form.Item>
                                    <Form.Item
                                        label="软件投入"
                                        name="softwareInvestment"
                                        rules={[{ required: true, message: '请输入软件投入' }]}
                                        style={{ marginBottom: 20 }}
                                    >
                                        <InputNumber placeholder="请输入软件投入" style={COMMON_FIELD_STYLE} />
                                    </Form.Item>

                                    <Form.Item
                                        label="云服务投入"
                                        name="cloudInvestment"
                                        rules={[{ required: true, message: '请输入云服务投入' }]}
                                        style={{ marginBottom: 20 }}
                                    >
                                        <InputNumber placeholder="请输入云服务投入" style={COMMON_FIELD_STYLE} />
                                    </Form.Item>
                                    <Form.Item
                                        label="设备投入"
                                        name="deviceInvestment"
                                        rules={[{ required: true, message: '请输入设备投入' }]}
                                        style={{ marginBottom: 20 }}
                                    >
                                        <InputNumber placeholder="请输入设备投入" style={COMMON_FIELD_STYLE} />
                                    </Form.Item>
                                    <Form.Item
                                        label="合同"
                                        name={"auditReport"}
                                        getValueProps={(values) => {
                                            return { values: values }
                                        }}
                                        rules={[{ required: true, message: '请上传合同' }]}
                                        style={{ marginBottom: 20 }}
                                    >
                                        <Upload
                                            fileList={hetongList}
                                            beforeUpload={(file) => {
                                                handleAutoUpload(file, "auditReport");
                                                return false;
                                            }}
                                            listType="text"
                                            maxCount={1}
                                            style={COMMON_FIELD_STYLE}
                                        >
                                            <Button icon={<UploadOutlined />}>点击上传合同</Button>
                                        </Upload>
                                    </Form.Item>
                                    <Form.Item
                                        label="发票"
                                        name={"invoiceReport"}
                                        getValueProps={(values) => {
                                            return { values: values }
                                        }}
                                        rules={[{ required: true, message: '请上传发票' }]}
                                        style={{ marginBottom: 20 }}
                                    >
                                        <Upload
                                            fileList={checkFilesList}
                                            beforeUpload={(file) => {
                                                handleAutoUpload(file, "invoiceReport");
                                                return false;
                                            }}
                                            listType="text"
                                            maxCount={1}
                                            style={COMMON_FIELD_STYLE}
                                        >
                                            <Button icon={<UploadOutlined />}>点击上传发票</Button>
                                        </Upload>
                                    </Form.Item>
                                    <Form.Item
                                        label="审计情况说明"
                                        name={"auditDescFiles"}
                                        getValueProps={(values) => {
                                            return { values: values }
                                        }}
                                        rules={[{ required: true, message: '请上传审计情况说明' }]}
                                        style={{ marginBottom: 20 }}
                                    >
                                        <Upload
                                            fileList={auditDescFilesList}
                                            beforeUpload={(file) => {
                                                handleAutoUpload(file, "auditDescFiles");
                                                return false;
                                            }}
                                            listType="text"
                                            maxCount={1}
                                            style={COMMON_FIELD_STYLE}
                                        >
                                            <Button icon={<UploadOutlined />}>点击上传审计情况说明</Button>
                                        </Upload>
                                    </Form.Item>
                                    <Form.Item
                                        label="付款凭证"
                                        name={"paywayFiles"}
                                        getValueProps={(values) => {
                                            return { values: values }
                                        }}
                                        rules={[{ required: true, message: '请上传付款凭证' }]}
                                        style={{ marginBottom: 20 }}
                                    >
                                        <Upload
                                            fileList={paywayFilesList}
                                            beforeUpload={(file) => {
                                                handleAutoUpload(file, "paywayFiles");
                                                return false;
                                            }}
                                            listType="text"
                                            maxCount={1}
                                            style={COMMON_FIELD_STYLE}
                                        >
                                            <Button icon={<UploadOutlined />}>点击上传付款凭证</Button>
                                        </Upload>
                                    </Form.Item>
                                    <Form.Item
                                        label="其他文件"
                                        name={"otherFiles"}
                                        getValueProps={(values) => {
                                            return { values: values }
                                        }}
                                        // rules={[{ required: true, message: '请上传付款凭证' }]}
                                        style={{ marginBottom: 20 }}
                                    >
                                        <Upload
                                            fileList={otherFilesList}
                                            beforeUpload={(file) => {
                                                handleAutoUpload(file, "otherFiles");
                                                return false;
                                            }}
                                            listType="text"
                                            maxCount={1}
                                            style={COMMON_FIELD_STYLE}
                                        >
                                            <Button icon={<UploadOutlined />}>点击上传其他文件</Button>
                                        </Upload>
                                    </Form.Item>
                                </>
                            )
                        }
                        {
                            initialState?.currentUser.data.userType == "accept" && (
                                <>
                                    <Form.Item
                                        label="评测时间"
                                        name={"rateTime"}
                                        rules={[{ required: true, message: '请选择评测时间' }]}
                                        style={{ marginBottom: 20 }}
                                    >
                                        <DatePicker />
                                    </Form.Item>
                                    <Form.Item
                                        label="是否已开展绩效评价"
                                        name={"rateDesc"}
                                        rules={[{ required: true, message: '请选择是否已开展绩效评价' }]}
                                        style={{ marginBottom: 20 }}
                                    >
                                        <Select
                                            placeholder="请选择是否已开展绩效评价"
                                            allowClear
                                            options={[
                                                { value: "是", label: "是" },
                                                { value: "否", label: "否" },
                                            ]}
                                        />
                                    </Form.Item>
                                    <Form.Item
                                        label="绩效评价是否通过"
                                        name={"rateThrough"}
                                        rules={[{ required: true, message: '请选择绩效评价是否通过' }]}
                                        style={{ marginBottom: 20 }}
                                    >
                                        <Select
                                            placeholder="请选择绩效评价是否通过"
                                            allowClear
                                            options={[
                                                { value: "是", label: "是" },
                                                { value: "否", label: "否" },
                                            ]}
                                        />
                                    </Form.Item>
                                    <Form.Item
                                        label="是否将该企业作为样板"
                                        name={"isMode"}
                                        rules={[{ required: true, message: '请选择是否将该企业作为样板' }]}
                                        style={{ marginBottom: 20 }}
                                    >
                                        <Select
                                            placeholder="请选择是否将该企业作为样板"
                                            allowClear
                                            onChange={(value) => {
                                                if (value == "是") {
                                                    setNeedReason(true)
                                                } else {
                                                    setNeedReason(false)
                                                }
                                            }}
                                            options={[
                                                { value: "是", label: "是" },
                                                { value: "否", label: "否" },
                                            ]}
                                        />
                                    </Form.Item>
                                    {
                                        needReason && (
                                            <Form.Item
                                                name="modeReason"
                                                label="理由"
                                                rules={[{ required: true, message: '请输入理由' }]}
                                                style={{ marginBottom: 20 }}
                                            >
                                                <TextArea
                                                    maxLength={500}
                                                    showCount
                                                    style={{
                                                        minHeight: 100,
                                                        resize: 'vertical',
                                                        ...COMMON_FIELD_STYLE,
                                                    }}
                                                />
                                            </Form.Item>
                                        )
                                    }

                                    <Form.Item
                                        name="effect"
                                        label="数字化基础、管理及成效得分"
                                        rules={[{ required: true, message: '请输入数字化基础、管理及成效得分', pattern: /^\d+(\.\d+)?$/ }]}
                                        style={{ marginBottom: 20 }}
                                    >
                                        <Input placeholder='请输入数字化基础、管理及成效得分' style={COMMON_FIELD_STYLE} />
                                    </Form.Item>

                                    <Form.Item
                                        label="验收报告"
                                        name={"acceptanceReport"}
                                        getValueProps={(values) => {
                                            return { values: values }
                                        }}
                                        rules={[{ required: true, message: '请上传验收报告' }]}
                                        style={{ marginBottom: 20 }}
                                    >
                                        <Upload
                                            fileList={hetongList}
                                            beforeUpload={(file) => {
                                                handleAutoUpload(file, "acceptanceReport");
                                                return false;
                                            }}
                                            listType="text"
                                            maxCount={1}
                                            style={COMMON_FIELD_STYLE}
                                        >
                                            <Button icon={<UploadOutlined />}>点击上传验收报告</Button>
                                        </Upload>
                                    </Form.Item>
                                </>
                            )
                        }
                    </div>
                </div>
            </Form>
        </Modal>
    )
}

export default CompanyAllInfosPage;